/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2010
 */

package com.hibernate.model;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.hibernate.javacommon.base.BaseEntity;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;

import cn.org.rapid_framework.util.DateConvertUtils;

/**
 * @author badqiu email:badqiu(a)gmail.com
 * @version 1.0
 * @since 1.0
 */


@Entity
@Table(name = "AD_PLAN")
public class AdPlan extends BaseEntity implements java.io.Serializable{
	private static final long serialVersionUID = 5454155825314635342L;
	
	//alias
	public static final String TABLE_ALIAS = "AdPlan";
	public static final String ALIAS_ID = "id";
	public static final String ALIAS_ORDER_ID = "订单ID";
	public static final String ALIAS_NAME = "名称";
	public static final String ALIAS_SORTING = "优先级";
	public static final String ALIAS_STATUS = "状态";
	public static final String ALIAS_CLICK_URL = "点击地址";
	public static final String ALIAS_START_TIME = "开始时间";
	public static final String ALIAS_END_TIME = "结束日期";
	public static final String ALIAS_PERIOD = "";
	public static final String ALIAS_PAY_TYPE = "支付类型";
	public static final String ALIAS_TOTAL_MAX = "曝光总量阀值";
	public static final String ALIAS_DAY_MAX = "曝光每日阀值";
	public static final String ALIAS_ONE_CPM_MAX = "单人CPM阀值";
	public static final String ALIAS_ONE_CPC_MAX = "单人CPC阀值";
	public static final String ALIAS_PRICE = "单价";
	public static final String ALIAS_DISCOUNT = "折扣";
	public static final String ALIAS_TOTAL_PRICE = "总价";
	public static final String ALIAS_CREATION_TIME = "创建时间";
	public static final String ALIAS_AREA_STR = "地域限制";
	public static final String ALIAS_PROPERTY_STR = "人群限制";
	public static final String ALIAS_MIN_CPM = "每日最低CPM";
	public static final String ALIAS_MIN_CPC = "每日最低CPC";
	public static final String ALIAS_LABLEID_STR = "lableidStr";
	public static final String ALIAS_ADWEBSITEID = "adwebsiteid";
	public static final String ALIAS_TIMESLOT = "时段";
	public static final String ALIAS_LZ_URL = "lzUrl";
	public static final String ALIAS_OTHER_CODE = "otherCode";
	public static final String ALIAS_DSP_FLAG = "类型";
	public static final String ALIAS_ONE_THRESHOLD_FLG = "阀值控制类型";
	public static final String ALIAS_REMARKS = "备注";
	public static final String ALIAS_PARAM_FLAG = "是否带参数";
	public static final String ALIAS_DZ_FLAG = "是否多帧";
	public static final String ALIAS_REF_FLAG = "refurl是否optimus";
	public static final String ALIAS_BID_POLICY = "出价策略";
	public static final String ALIAS_AGENT_LIST = "浏览器定向";
	public static final String ALIAS_AD_LOCATION_LIST = "广告位位置";
	public static final String ALIAS_URL_BLACK_LIST = "URL黑名单";
	public static final String ALIAS_URL_WHITE_LIST = "URL白名单";
	public static final String ALIAS_INCR_PRICE = "每次加价";
	public static final String ALIAS_EXCHANGE_LIST = "AdExchange";
	public static final String ALIAS_BID_PRICE = "最高出价";
	public static final String ALIAS_DAY_AMOUNT_LIMIT = "每日最高消费";
	public static final String ALIAS_TOTAL_CPC_MAX = "点击总量上限";
	public static final String ALIAS_DAY_CPC_MAX = "每日点击上限";
	public static final String ALIAS_CPM_MAX_UNIT_PRICE = "一个CPM最高单价";
	public static final String ALIAS_CPC_MAX_UNIT_PRICE = "一个点击最高单价";
	
	//date formats
	public static final String FORMAT_START_TIME = DATE_FORMAT;
	public static final String FORMAT_END_TIME = DATE_FORMAT;
	public static final String FORMAT_CREATION_TIME = DATE_TIME_FORMAT;
	
	public static final String FORMAT_START_TIME_SLOT = TIME_FORMAT;
	public static final String FORMAT_END_TIME_SLOT = TIME_FORMAT;
	

	//可以直接使用: @Length(max=50,message="用户名长度不能大于50")显示错误消息
	//columns START
	
	private Long id;
	@NotNull 
	private Long orderId;
	@Length(max=255)
	private String otherCode;
	@NotBlank @Length(max=50)
	private java.lang.String name;
	@NotBlank @Length(max=1)
	private java.lang.String flow;
	@NotBlank @Length(max=2)
	private java.lang.String sorting;
	/**
	 * 0 准备中
	 * 1 待投放
	 * 2 投放中
	 * 3 已结束
	 * 4 已停止
	 * 5 已暂停
	 */
	@NotBlank @Length(max=1)
	private java.lang.String status;
	@NotBlank @Length(max=200)
	private java.lang.String clickUrl;
	@NotNull 
	private java.util.Date startTime;
	@NotNull 
	private java.util.Date endTime;
	
	@NotBlank @Length(max=1)
	private java.lang.String period;
	@NotBlank @Length(max=1)
	private java.lang.String payType;
	@NotNull 
	private Long totalMax;       //总量上限
	@NotNull 
	private Long dayMax;         //每日上限
	@NotNull 
	private Long oneCpmMax;      //单人曝光上限
	@NotNull 
	private Long oneCpcMax;      //单人点击上限
	@NotNull 
	private Long price;          //
	@NotNull 
	private Long discount;
	
	private java.util.Date creationTime;
	
	private String areaStr;
	
	private String propertyStr;   //精准定向字符串拼接
	
	private String timeslot;      
	private Long minCpm;          //每日曝光至少cpm
	private Long minCpc;          //每日曝光至少cpc
	
	private String adwebsiteid;
	private String lableidStr;
	
	
	private String endTimeslotString;
	
	private String startTimeslotString;
	
	private Integer dspFlag;
	
	private String remarks;
	
	private Integer paramFlag;
	
	private Integer dzFlag;
	
	private String oneThresholdFlg;
	
	private String refFlag;
	
	
	/** 浏览器定向,多个浏览器中间以逗号分隔*/
	@Length(max=1000)
	private String agentList;
	/** 广告位置\n1.第一屏\n2.其它 */
	@Length(max=100)
	private String adLocationList;
	/** url黑名单 */
	@Length(max=5000)
	private String urlBlackList;
	/** url白名单 */
	@Length(max=5000)
	private String urlWhiteList;
	/** AdExchange选择\n包括adx、tanx */
	@Length(max=1000)
	private String exchangeList;
	/** 出价策略\n1.固定出价\n2.最低价浮动\n3.智能出价\n */
	private Integer bidPolicy;
	/** 浮动出价，每次加价 */
	private Double incrPrice;
	/** 竞价固定出价，最高价 */
	private Double bidPrice;
	/** 每日消费上限 */
	private Double dayAmountLimit;
	/** 消费总量上限 */
	@NotNull 
	private Double totalPrice;
	/** 点击总量上限 */
	private Long totalCpcMax;
	/** 每日点击上限 */
	private Long dayCpcMax;
	/** 点击单价 */
	private Double cpcMaxUnitPrice;
	
	
	//columns END


	public AdPlan(){
	}

	public AdPlan(
		Long id
	){
		this.id = id;
	}
	
	@Column(name = "DSP_FLAG", unique = false, insertable = true, updatable = false, length = 1)
	public Integer getDspFlag() {
		return dspFlag;
	}

	public void setDspFlag(Integer dspFlag) {
		this.dspFlag = dspFlag;
	}
	
	@Column(name = "LABLEID_STR")
	public String getLableidStr() {
		return lableidStr;
	}

	public void setLableidStr(String lableidStr) {
		this.lableidStr = lableidStr;
	}

	public void setId(Long value) {
		this.id = value;
	}
	@Id
	public Long getId() {
		return this.id;
	}
	
	@Column(name = "ORDER_ID", unique = false, insertable = true, updatable = true, length = 20)
	public Long getOrderId() {
		return this.orderId;
	}
	
	public void setOrderId(Long value) {
		this.orderId = value;
	}
	
	@Column(name = "NAME", unique = false, insertable = true, updatable = true, length = 50)
	public String getName() {
		return this.name;
	}
	
	public void setName(String value) {
		this.name = value;
	}
	
	@Column(name = "FLOW", unique = false, insertable = true, updatable = true, length = 1)
	public String getFlow() {
		return this.flow;
	}
	
	public void setFlow(String value) {
		this.flow = value;
	}
	@Column(name = "area_str")
	public String getAreaStr() {
		return areaStr;
	}

	public void setAreaStr(String areaStr) {
		this.areaStr = areaStr;
	}
	@Column(name = "property_str")
	public String getPropertyStr() {
		return propertyStr;
	}

	@Column(name = "REF_FLAG")
	public String getRefFlag() {
		return refFlag;
	}

	public void setRefFlag(String refFlag) {
		this.refFlag = refFlag;
	}

	public void setPropertyStr(String propertyStr) {
		this.propertyStr = propertyStr;
	}

	@Column(name = "SORTING", unique = false, insertable = true, updatable = true, length = 2)
	public String getSorting() {
		return this.sorting;
	}
	
	public void setSorting(String value) {
		this.sorting = value;
	}

	@Column(name = "STATUS", unique = false,insertable = true, updatable = true, length = 1)
	public String getStatus() {
		return this.status;
	}
	
	public void setStatus(String value) {
		this.status = value;
	}
	
	@Column(name = "CLICK_URL", unique = false, insertable = true, updatable = true, length = 200)
	public String getClickUrl() {
		return this.clickUrl;
	}
	
	public void setClickUrl(String value) {
		this.clickUrl = value;
	}
	
	//begin add by wangweiwei 量子统计URL 2011/9/28
	private String lzUrl;
	@Column(name="LZ_URL" ,unique=false, insertable = true, updatable = true, length = 1000)
	public String getLzUrl(){
		return this.lzUrl;
	}

	public void setLzUrl(String value){
		this.lzUrl = value;
	}
	//end
	
	@Transient
	public String getStartTimeString() {
		return DateConvertUtils.format(getStartTime(), FORMAT_START_TIME);
	}
	public void setStartTimeString(String value) {
		setStartTime(DateConvertUtils.parse(value, FORMAT_START_TIME,java.util.Date.class));
	}
	
	@Column(name = "START_TIME", unique = false, insertable = true, updatable = true, length = 7)
	public java.util.Date getStartTime() {
		return this.startTime;
	}
	
	public void setStartTime(java.util.Date value) {
		this.startTime = value;
	}
	
	@Transient
	public String getEndTimeString() {
		return DateConvertUtils.format(getEndTime(), FORMAT_END_TIME);
	}
	public void setEndTimeString(String value) {
		setEndTime(DateConvertUtils.parse(value, FORMAT_END_TIME,java.util.Date.class));
	}

	@Column(name = "END_TIME", unique = false, insertable = true, updatable = true, length = 7)
	public java.util.Date getEndTime() {
		return this.endTime;
	}
	
	public void setEndTime(java.util.Date value) {
		this.endTime = value;
	}
	
	@Transient
	public String getStartTimeslotString() {
		return startTimeslotString;
	}
	public void setStartTimeslotString(String value) {
		this.startTimeslotString = value;
	}
	
	@Transient
	public String getEndTimeslotString() {
		return endTimeslotString;
	}
	public void setEndTimeslotString(String value) {
		this.endTimeslotString = value;
	}
	
	@Column(name = "PERIOD", unique = false, insertable = true, updatable = true, length = 1)
	public String getPeriod() {
		return this.period;
	}
	
	public void setPeriod(String value) {
		this.period = value;
	}
	
	@Column(name = "PAY_TYPE", unique = false, insertable = true, updatable = true, length = 1)
	public String getPayType() {
		return this.payType;
	}
	
	public void setPayType(String value) {
		this.payType = value;
	}
	
	@Column(name = "TOTAL_MAX", unique = false, insertable = true, updatable = true, length = 20)
	public Long getTotalMax() {
		return this.totalMax;
	}
	
	public void setTotalMax(Long value) {
		this.totalMax = value;
	}
	
	@Column(name = "DAY_MAX", unique = false, insertable = true, updatable = true, length = 20)
	public Long getDayMax() {
		return this.dayMax;
	}
	
	public void setDayMax(Long value) {
		this.dayMax = value;
	}
	
	@Column(name = "ONE_CPM_MAX", unique = false, insertable = true, updatable = true, length = 20)
	public Long getOneCpmMax() {
		return this.oneCpmMax;
	}
	
	public void setOneCpmMax(Long value) {
		this.oneCpmMax = value;
	}
	
	@Column(name = "ONE_CPC_MAX", unique = false, insertable = true, updatable = true, length = 20)
	public Long getOneCpcMax() {
		return this.oneCpcMax;
	}
	
	public void setOneCpcMax(Long value) {
		this.oneCpcMax = value;
	}
	
	@Column(name = "PRICE", unique = false, insertable = true, updatable = true, length = 18)
	public Long getPrice() {
		return this.price;
	}
	
	public void setPrice(Long value) {
		this.price = value;
	}
	
	@Column(name = "DISCOUNT", unique = false, insertable = true, updatable = true, length = 10)
	public Long getDiscount() {
		return this.discount;
	}
	
	public void setDiscount(Long value) {
		this.discount = value;
	}
	
	@Column(name = "TOTAL_PRICE", unique = false, insertable = true, updatable = true, length = 18)
	public Double getTotalPrice() {
		return totalPrice;
	}
	
	public void setTotalPrice(Double value) {
		this.totalPrice = value;
	}
	
	@Column(name = "MIN_CPM", unique = false, insertable = true, updatable = true, length = 20)
	public Long getMinCpm() {
		return minCpm;
	}

	public void setMinCpm(Long minCpm) {
		this.minCpm = minCpm;
	}
	
	@Column(name = "MIN_CPC", unique = false, insertable = true, updatable = true, length = 20)
	public Long getMinCpc() {
		return minCpc;
	}

	public void setMinCpc(Long minCpc) {
		this.minCpc = minCpc;
	}
	
	
	@Transient
	public String getCreationTimeString() {
		return DateConvertUtils.format(getCreationTime(), FORMAT_CREATION_TIME);
	}
	public void setCreationTimeString(String value) {
		setCreationTime(DateConvertUtils.parse(value, FORMAT_CREATION_TIME,java.util.Date.class));
	}
	
	@Column(name = "CREATION_TIME", unique = false, insertable = true, length = 7)
	public java.util.Date getCreationTime() {
		return this.creationTime;
	}
	
	@Column(name="PARAM_FLAG")
	public Integer getParamFlag() {
		return paramFlag;
	}

	public void setParamFlag(Integer paramFlag) {
		this.paramFlag = paramFlag;
	}
	
	
	@Column(name="ONE_THRESHOLD_FLG")
	public String getOneThresholdFlg() {
		return oneThresholdFlg;
	}

	public void setOneThresholdFlg(String oneThresholdFlg) {
		this.oneThresholdFlg = oneThresholdFlg;
	}

	@Column(name="DZ_FLAG")
	public Integer getDzFlag() {
		return dzFlag;
	}

	public void setDzFlag(Integer dzFlag) {
		this.dzFlag = dzFlag;
	}

	public void setCreationTime(java.util.Date value) {
		this.creationTime = value;
	}
	
	@Column(name = "REMARKS")
	public String getRemarks() {
		return remarks;
	}

	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}

	@Column(name = "ADWEBSITEID")

	public String getAdwebsiteid() {
		return adwebsiteid;
	}

	public void setAdwebsiteid(String adwebsiteid) {
		this.adwebsiteid = adwebsiteid;
	}

	@Column(name = "TIMESLOT")
	public String getTimeslot() {
		return timeslot;
	}
	               
	public void setTimeslot(String timeslot) {
		this.timeslot = timeslot;
	}

	private Set materials = new HashSet(0);
	public void setMaterials(Set<Material> material){
		this.materials = material;
	}
	
	@OneToMany(cascade = { CascadeType.MERGE }, fetch = FetchType.LAZY, mappedBy = "adPlan")
	public Set<Material> getMaterials() {
		return materials;
	}
	
	private AdOrder adOrder;
	public void setAdOrder(AdOrder adOrder){
		if(adOrder != null)
			adOrder.getName();
		this.adOrder = adOrder;
	}
	
	@ManyToOne(cascade = {}, fetch = FetchType.LAZY )
	@JoinColumns({
		@JoinColumn(name = "ORDER_ID", insertable = false, updatable = false) 
	})
	public AdOrder getAdOrder() {
		return adOrder;
	}
	
	@Column(name = "OTHER_CODE", unique = false, nullable = true, insertable = true, updatable = true, length = 255)
	public String getOtherCode() {
		return this.otherCode;
	}
	
	public void setOtherCode(String value) {
		this.otherCode = value;
	}
	
	@Column(name = "BID_POLICY", unique = false, nullable = true, insertable = true, updatable = true, length = 10)
	public Integer getBidPolicy() {
		return this.bidPolicy;
	}
	
	public void setBidPolicy(Integer value) {
		this.bidPolicy = value;
	}
	
	@Column(name = "AGENT_LIST", unique = false, nullable = true, insertable = true, updatable = true, length = 1000)
	public String getAgentList() {
		return this.agentList;
	}
	
	public void setAgentList(String value) {
		this.agentList = value;
	}
	
	@Column(name = "AD_LOCATION_LIST", unique = false, nullable = true, insertable = true, updatable = true, length = 100)
	public String getAdLocationList() {
		return this.adLocationList;
	}
	
	public void setAdLocationList(String value) {
		this.adLocationList = value;
	}
	
	@Column(name = "URL_BLACK_LIST", unique = false, nullable = true, insertable = true, updatable = true, length = 5000)
	public String getUrlBlackList() {
		return this.urlBlackList;
	}
	
	public void setUrlBlackList(String value) {
		this.urlBlackList = value;
	}
	
	@Column(name = "URL_WHITE_LIST", unique = false, nullable = true, insertable = true, updatable = true, length = 5000)
	public String getUrlWhiteList() {
		return this.urlWhiteList;
	}
	
	public void setUrlWhiteList(String value) {
		this.urlWhiteList = value;
	}
	
	@Column(name = "INCR_PRICE", unique = false, nullable = true, insertable = true, updatable = true, length = 18)
	public Double getIncrPrice() {
		return incrPrice;
	}
	
	public void setIncrPrice(Double value) {
		this.incrPrice = value;
	}
	
	@Column(name = "EXCHANGE_LIST", unique = false, nullable = true, insertable = true, updatable = true, length = 1000)
	public String getExchangeList() {
		return this.exchangeList;
	}
	
	public void setExchangeList(String value) {
		this.exchangeList = value;
	}
	
	@Column(name = "BID_PRICE", unique = false, nullable = true, insertable = true, updatable = true, length = 18)
	public Double getBidPrice() {
		return bidPrice;
	}
	
	public void setBidPrice(Double value) {
		this.bidPrice = value;
	}
	
	@Column(name = "DAY_AMOUNT_LIMIT", unique = false, nullable = true, insertable = true, updatable = true, length = 18)
	public Double getDayAmountLimit() {
		return this.dayAmountLimit;
	}
	
	public void setDayAmountLimit(Double value) {
		this.dayAmountLimit = value;
	}
	

	@Column(name = "TOTAL_CPC_MAX", unique = false, nullable = true, insertable = true, updatable = true, length = 20)
	public Long getTotalCpcMax() {
		return this.totalCpcMax;
	}
	
	public void setTotalCpcMax(Long value) {
		this.totalCpcMax = value;
	}
	
	@Column(name = "DAY_CPC_MAX", unique = false, nullable = true, insertable = true, updatable = true, length = 20)
	public Long getDayCpcMax() {
		return this.dayCpcMax;
	}
	
	public void setDayCpcMax(Long value) {
		this.dayCpcMax = value;
	}
	
	
	public String toString() {
		return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
		.append("Id",getId())
		.append("OrderId",getOrderId())
		.append("Name",getName())
		.append("Flow",getFlow())
		.append("Sorting",getSorting())
		.append("Status",getStatus())
		.append("ClickUrl",getClickUrl())
		.append("StartTime",getStartTime())
		.append("EndTime",getEndTime())
		.append("Period",getPeriod())
		.append("PayType",getPayType())
		.append("TotalMax",getTotalMax())
		.append("DayMax",getDayMax())
		.append("OneCpmMax",getOneCpmMax())
		.append("OneCpcMax",getOneCpcMax())
		.append("Price",getPrice())
		.append("Discount",getDiscount())
		.append("TotalPrice",getTotalPrice())
		.append("CreationTime",getCreationTime())
		.append("AreaStr",getAreaStr())
		.append("PropertyStr",getPropertyStr())
		.append("MinCpm",getMinCpm())
		.append("MinCpc",getMinCpc())
		.append("Paytype",getPayType())
		.append("LableidStr",getLableidStr())
		.append("Adwebsiteid",getAdwebsiteid())
		.append("Timeslot",getTimeslot())
		.append("LzUrl",getLzUrl())
		.append("OtherCode",getOtherCode())
		.append("DspFlag",getDspFlag())
		.append("OneThresholdFlg",getOneThresholdFlg())
		.append("Remarks",getRemarks())
		.append("ParamFlag",getParamFlag())
		.append("DzFlag",getDzFlag())
		.append("RefFlag",getRefFlag())
		.append("BidPolicy",getBidPolicy())
		.append("AgentList",getAgentList())
		.append("AdLocationList",getAdLocationList())
		.append("UrlBlackList",getUrlBlackList())
		.append("UrlWhiteList",getUrlWhiteList())
		.append("IncrPrice",getIncrPrice())
		.append("ExchangeList",getExchangeList())
		.append("BidPrice",getBidPrice())
		.append("DayAmountLimit",getDayAmountLimit())
		.append("TotalCpcMax",getTotalCpcMax())
		.append("DayCpcMax",getDayCpcMax())
		.append("CpcMaxUnitPrice", getCpcMaxUnitPrice())
		.toString();
	}
	
	public int hashCode() {
		return new HashCodeBuilder()
			.append(getId())
			.toHashCode();
	}
	
	public boolean equals(Object obj) {
		if(obj instanceof AdPlan == false) return false;
		if(this == obj) return true;
		AdPlan other = (AdPlan)obj;
		return new EqualsBuilder()
			.append(getId(),other.getId())
			.isEquals();
	}
	@Transient
	@Override
	public String getTarget() {
		// TODO Auto-generated method stub
		return "计划";
	}
	@Transient
	@Override
	public Long getTargetId() {
		// TODO Auto-generated method stub
		return getId();
	}

	public void setCpcMaxUnitPrice(Double cpcMaxUnitPrice) {
		this.cpcMaxUnitPrice = cpcMaxUnitPrice;
	}

	@Column(name = "CPC_MAX_UNIT_PRICE", unique = false, insertable = true, updatable = true, length = 20)
	public Double getCpcMaxUnitPrice() {
		return cpcMaxUnitPrice;
	}
	@Transient
	@Override
	public String getTargetName() {
		// TODO Auto-generated method stub
		return getName();
	}
}

